package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 河北新闻网解析
 * @author Persh
 *
 */
public class HebNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.hebnews.cn/[0-9]{4}-[0-9]{2}/[0-9]{2}/.*_[0-9]*.htm",
		"http://news.hebnews.cn/.*/[0-9]*.html",
		"http://.*.hebnews.cn/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/.*_[0-9]*.htm",
		"http://game.hebnews.cn/.*/[0-9]*.html",
		"http://.*.hebnews.cn/[0-9]{6}/[0-9]{2}/.*_[0-9]*.html"};


	
	public boolean isDetailPage(String url) {
		if (url.startsWith("http://v.hebnews.cn")){
			return false;
		}
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
			title =doc.select("div.contentmain>h1").text();
			if(title==null || title.trim().length()==0){
				title =doc.select("div.artical>h1").text();
				if(title==null || title.trim().length()==0){
					title =doc.select("div#title").text();
					if(title==null || title.trim().length()==0){
						title =doc.select("h2.center").text();
						if(title==null || title.trim().length()==0){
							title =doc.select("div.hd>h1>strong").text();
							if(title==null || title.trim().length()==0){
								title =doc.select("div.Bil1>h1").text();
								if(title==null || title.trim().length()==0){
									title =doc.select("div.content>h1").text();
									if(title==null || title.trim().length()==0){
										title =doc.select("div#main-title").text();
										if(title==null || title.trim().length()==0){
										}
									}
								}
							}
						}
					}
				}
			}
			if(StringUtils.isBlank(title)) {
				title = doc.select("h1.title").text();
			}
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.contentbox > p"));
				if(content==null || content.trim().length()==0){
					content=HtmlCleaner.getContentHtml(url,doc.select("#zoom"));
					if(content==null || content.trim().length()==0){
						content=HtmlCleaner.getContentHtml(url,doc.select("div.content_info"));
						if(content==null || content.trim().length()==0){
							content=HtmlCleaner.getContentHtml(url,doc.select("div.newsmain"));
							if(content==null || content.trim().length()==0){
								content=HtmlCleaner.getContentHtml(url,doc.select("#icontent"));
								if(content==null || content.trim().length()==0){
									content=HtmlCleaner.getContentHtml(url,doc.select("div.news"));
									if(content==null || content.trim().length()==0){
										content=HtmlCleaner.getContentHtml(url,doc.select("div.relative"));
										if(content==null || content.trim().length()==0){
											content=HtmlCleaner.getContentHtml(url,doc.select("div.contentmain p"));
											if(content==null || content.trim().length()==0){
												content=HtmlCleaner.getContentHtml(url,doc.select("div.text"));
											}
										}
									}
								}
							}
						}
					}
				}
			}
			content=content.replace("正在加载中... ", "");

			String tempdate=doc.select("li.subleft").text();
			if(tempdate==null || tempdate.trim().length()==0){
				tempdate=doc.select("span.info").text();
				if(tempdate==null || tempdate.trim().length()==0){
					tempdate=doc.select("div#relate>div.web").text();
					if(tempdate==null || tempdate.trim().length()==0){
						tempdate=doc.select("div.sub_tit").text();
						if(tempdate==null || tempdate.trim().length()==0){
							tempdate=doc.select("span.time>strong").text();
							if(tempdate==null || tempdate.trim().length()==0){
								tempdate=doc.select("div.sou>a").text();
								if(tempdate==null || tempdate.trim().length()==0){
									tempdate=doc.select("div.content>ul>li").text();
									if(tempdate==null || tempdate.trim().length()==0){
										tempdate=doc.select("div.art_info").text();
									}
								}
							}
						}
					}
				}
			}
			date=DateUtils.matchDate(tempdate);
			if(null == date) {
				date = DateUtils.matchDate(doc.select("div.source").text());
			}
			if (date==null){
				date=DateUtils.matchDate(doc.select("span.pubtime001").text());
			}
			String commentNumstr=doc.select("span#commentCount").text();
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);
			if(commentNumstr!=null && commentNumstr.trim().length()!=0){
				Integer commentNum=0;
				commentNum=Integer.parseInt(commentNumstr);
				news.setCommentNum(commentNum);
			}
			String author=doc.select("div.contentmain ul.subtitle li.subleft").text();
			if(author!=null && author.indexOf("来源：")!=-1){
				author=StringUtils.substringAfter(author, "来源：");
			}
			news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		HebNewsAnalyse analyse=new HebNewsAnalyse();
		String url="http://bazhou.hebnews.cn/2014-03/25/content_3848019.htm";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
